// 辅助功能模块

import { observer } from "mobx-react";
import "./index.less"
import { Card, Spin, Button, Row, Col, Form, Input, Divider, Descriptions, Table, Alert } from 'antd';
import { PlayCircleOutlined } from '@ant-design/icons';
import store from "./model.js"
import { history } from "umi";
import Scan from "../publicComponent/scan"
import { useEffect, useState } from "react";
import indexStore from "../model"
import FingerApi from "../finger_device/model"
import { getDictLabel } from "@/utils/utils";


function Index() {

    // let [timer, setTimer] = useState(null)

    // useEffect(() => {
    //     store.allocation()
    // }, [])


    // 30s更新一次当日指纹
    let timer = null
    useEffect(() => {
        indexStore.dayFingerprint()
        timer = setInterval(() => {
            indexStore.dayFingerprint()
        }, 30000)
        // 销毁定时器
        return function () {
            if (timer) {
                clearInterval(timer)
            }
        }
    }, [])


    // 进入页面就开始扫描
    useEffect(() => {
        // 默认扫描不停止
        store.isStop = false
        store.contrast()
    }, [])

    // 记录最后一次跳转路由path
    // 设置窗口大小和位置
    useEffect(() => {
        indexStore.StoragePath(history.location.pathname)
        FingerApi.setWindowSize(450, 600)
        FingerApi.rightBottomWindow()
    }, [])

    // 处方表头
    let columns = [
        {
            title: '处方名',
            dataIndex: 'planName',
            align: "center"
        },
        {
            title: '诊疗技术',
            // dataIndex: 'technology',
            align: "center",
            render: function(text, row){
                return (
                    <span>{getDictLabel("code_technology", row.technology)}</span>
                )
            }
        },
        {
            title: '剩余未诊疗',
            align: "center",
            render: function (e, row) {
                return (
                    <span>
                        <a>{row.residueNumberOfTimes}</a>/<span>{row.numberOfTimes}</span>
                    </span>
                )
            }
        },
        // {
        //     title: '开发时间',
        //     dataIndex: 'createTime',
        //     align: "center"
        // },
        // {
        //     title: '医师',
        //     dataIndex: 'createUser',
        //     align: "center"
        // },
        {
            title: '是否已完成',
            align: "center",
            render: function (e, row) {
                return (
                    <span>
                        {/* <a >完成</a> */}
                        <Button size="small" onClick={() => { store.changeState(row.id) }} disabled={store.isdisabled} type="" >完成</Button>
                    </span>
                )
            }
        },
    ]

    let clickNum = 0
    let time = null
    // 连点3次才能退出
    function goback() {
        if (clickNum == 0) {
            clickNum += 1
            time = new Date().getTime()
        } else {
            if ((new Date().getTime() - time) < 800) {
                clickNum += 1
                time = new Date().getTime()
            } else {
                clickNum = 0
            }
        }
        if (clickNum === 3) {
            history.push({ pathname: "/", query: { name: "child" } })
            indexStore.StoragePath("/")
            store.isStop = true
        }
    }

    return (
        <>
            <Spin tip="加载中" spinning={store.loading} size="large">
                <div className="assist">
                    <div className="assist-title">
                        <span onClick={goback}>其它方式</span>
                    </div>
                    <div className="assist-body">
                        {/* 患者详情 */}

                        <div className="assist-body-detail">
                            {
                                store.isfinger
                                    ?
                                    (store.prescriptionList.length
                                        ?
                                        <>
                                            <Divider style={{ fontSize: "25px" }} children={`${store.patientInfo.name}您好`} />
                                            <Table
                                                bordered
                                                pagination={false}
                                                rowKey="id"
                                                size="small"
                                                columns={columns}
                                                dataSource={store.prescriptionList}
                                            />
                                        </>
                                        :
                                        store.patientInfo.name === ""
                                            ? ""
                                            :
                                            <Alert style={{ fontSize: "20px" }} message={`${store.patientInfo.name}您好！您当前没有处方！！！`} type="error" />
                                    )
                                    : <Alert style={{ fontSize: "20px" }} message="您没有录入指纹！！！" type="error" />
                            }
                        </div>


                    </div>
                </div>
            </Spin>
        </>
    )
}

export default observer(Index)